<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
    }
    body {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-color: #2b88f0;
      overflow: hidden;
      margin: 0;
      color: #fff;
      font-family: 'Muli', sans-serif;
    }
    .container {
      width: 500px;
    }
    textarea {
      border: none;
      display: block;
      width: 100%;
      height: 100px;
      margin: 0 0 20px;
      font-size: 16px;
    }
    textarea:focus {
      outline: none;
    }
    .tag {
      display: inline-block;
      background-color: #f0932b;
      color: #fff;
      border-radius: 50px;
      padding: 10px 20px;
      margin: 0 5px 20px 0;
      font-size: 14px;
    }
    .tag.highlight {
      background-color: #273c75;
    }
  </style>
</head>
<body>
  <div class="container">
    <h3>输入值用逗号","分隔, 输入完毕按回车</h3>
    <textarea id="textarea" placeholder="输入完毕按回车，用逗号进行每一项分隔"></textarea>
    <div id="tags"></div>
  </div>

  <script>
    const tagsEl = document.getElementById("tags");
    const textarea = document.getElementById("textarea");

    textarea.focus();

    textarea.addEventListener("keyup", (event) => {
      createTags(event.target.value);

      if(event.key === "Enter") {
        setTimeout(() => {
          event.target.value = "";
        }, 10); 

        randomSelect();
      };

      console.log("jj")
    });

    function createTags(input) {
      const tags = input.split(",").filter(tag => tag.trim() !== "").map(tag => tag.trim());
      tagsEl.innerHTML = "";
      tags.forEach(tag => {
        const tagEl = document.createElement("span");
        tagEl.classList.add("tag");
        tagEl.innerHTML = tag;
        tagsEl.appendChild(tagEl);
      });
    };

    function randomSelect() {
      const times = 30;
      const interval = setInterval(() => {
        const randomTag = pickRandomTag();

        if(randomTag !== undefined) {
          highlightTag(randomTag);

          setTimeout(() => {
            unHighLightTag(randomTag);
          }, 100)
        };
      }, 100)

      setTimeout(() => {
        clearInterval(interval)

        setTimeout(() => {
            const randomTag = pickRandomTag()

            highlightTag(randomTag)
        }, 100)

      }, times * 100)
    };

    function pickRandomTag() {
      const tags = document.querySelectorAll(".tag");
      return tags[Math.floor(Math.random() * tags.length)];
    };

    function highlightTag(tag) {
      tag.classList.add("highlight");
    };

    function unHighLightTag(tag) {
      tag.classList.remove("highlight");
    }
  </script>
</body>
</html>